package systempen.cmanager;

import systempen.cmanager.DialogAccountBrowser.IFoundAccount;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Settings extends ListActivity{

	public TextView etInsert;
	public String defaultPeriod;
	public String defaultAccount;

	@Override
	public void onCreate(Bundle icicle){
		super.onCreate(icicle);
		ThemePicker.onActivityCreateSetTheme(this);
		try{
			setContentView(R.layout.settings);
			//Bundle bun = getIntent().getExtras();
			etInsert = (TextView) findViewById(R.id.textLastmonth);
			etInsert.setText(util.GetLastMonthPeriodDB().a.toString());
			etInsert.setText(etInsert.getText()+ " :|:" +util.GetLastMonthPeriodDB().b.toString());
			defaultPeriod = "Default period: "+util.getFirstDataString("select v from setings where k = \'DEFAULTPERIOD\'", getApplicationContext());
			defaultAccount = "Default account:"+util.getDefaultAccountName(getApplicationContext());
			String[] names = new String[] {defaultPeriod,defaultAccount,"Reset order of types","Rate Cost Manager","Delete all budgets"};

			// Create an ArrayAdapter, that will actually make the Strings above
			// appear in the ListView
			this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names));
		}//od try
		catch(Exception ex) 
		{
			Toast.makeText(this,"Error in settings: "+ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String keyword = o.toString();
		Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
		String substr; 
		substr = keyword.substring(0, 14);
		if (substr.equals("Default period")){
			final CharSequence[] items = {"This month","Last month","Today","This week","All time"};

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Choose period");
			builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
					String sql;
					if (items[item].equals("This month")){
						sql = "update setings set v = \'THISMONTH\' where k = \'DEFAULTPERIOD\'";
						util.execSQL(sql, getApplicationContext());
					}
					if (items[item].equals("Last month")){
						sql = "update setings set v = \'LASTMONTH\' where k = \'DEFAULTPERIOD\'";
						util.execSQL(sql, getApplicationContext());
					}
					if (items[item].equals("Today")){
						sql = "update setings set v = \'TODAY\' where k = \'DEFAULTPERIOD\'";
						util.execSQL(sql, getApplicationContext());
					}
					if (items[item].equals("All time")){
						sql = "update setings set v = \'ALLTIME\' where k = \'DEFAULTPERIOD\'";
						util.execSQL(sql, getApplicationContext());
					}
					if (items[item].equals("This week")){
						sql = "update setings set v = \'THISWEEK\' where k = \'DEFAULTPERIOD\'";
						util.execSQL(sql, getApplicationContext());
					}


					defaultPeriod = "Default period: "+util.getFirstDataString("select v from setings where k = \'DEFAULTPERIOD\'", getApplicationContext());
					String[] names = new String[] {defaultPeriod,defaultAccount,"Reset order of types","Rate Cost Manager","Delete all budgets"};
					setListAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, names));
					dialog.dismiss();
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
		}
		
		if (substr.equals("Delete all bud")){
			String sql;
			sql = "delete from budget";
			util.execSQL(sql, getApplicationContext());
			Toast.makeText(this,"Budgets deleted.", Toast.LENGTH_LONG).show();
		}

		if (substr.equals("Reset order of")){
			String sql;
			sql = "update cost_type set orde = code";
			util.execSQL(sql, getApplicationContext());
			Toast.makeText(this,"Folder order resetted.", Toast.LENGTH_LONG).show();
		}
		if (substr.equals("Rate Cost Mana")){
			Intent goToMarket = null;
			goToMarket = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=systempen.cmanager"));
			startActivity(goToMarket);
		}
		if (substr.equals("Default accoun")){
			IFoundAccount ifa = new IFoundAccount() {

				@Override
				public void onFoundAccount(String account) {
					// TODO Auto-generated method stub
					String sql;
					sql = "update setings set v = "+account.toString()+" where k = \'DEFAULTACCOUNT\'";
					util.execSQL(sql, getApplicationContext());

					defaultAccount = "Default account:"+util.getDefaultAccountName(getApplicationContext());
					String[] names = new String[] {defaultPeriod,defaultAccount,"Reset order of types","Rate Cost Manager","Delete all budgets"};
					setListAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, names));
				}
			};
			DialogAccountBrowser dab = new DialogAccountBrowser(Settings.this, ifa);
			dab.show();

		}
	}
}
